/*******************************************************************************
 * Copyright (c) 2017-2020, org.smartboot. All rights reserved.
 * project name: smart-http
 * file name: SimpleSmartHttp.java
 * Date: 2020-04-03
 * Author: sandao (zhengjunweimail@163.com)
 ******************************************************************************/

package org.smartboot.fanyun;

import org.smartboot.http.HttpBootstrap;
import org.smartboot.http.HttpRequest;
import org.smartboot.http.HttpResponse;
import org.smartboot.http.WebSocketRequest;
import org.smartboot.http.WebSocketResponse;
import org.smartboot.http.logging.RunLogger;
import org.smartboot.http.server.handle.HttpHandle;
import org.smartboot.http.server.handle.HttpRouteHandle;
import org.smartboot.http.server.handle.WebSocketDefaultHandle;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.logging.Level;

/**
 * 监控8080端口POST请求
 *
 * @author chenmin
 * @version V1.0 , 2020/11/07
 */
public class SimpleSmartHttp {
    public static void main(String[] args) {
        HttpRouteHandle routeHandle = new HttpRouteHandle();
        routeHandle.route("/", new HttpHandle() {
            @Override
            public void doHandle(HttpRequest request, HttpResponse response) throws IOException {
                RunLogger.getLogger().log(Level.INFO,"/*************请求头信息*************/");
                RunLogger.getLogger().log(Level.INFO,"ContentType："+request.getContentType());
                RunLogger.getLogger().log(Level.INFO,"ContentType："+request.getContentLength());
                RunLogger.getLogger().log(Level.INFO,"CharacterEncoding："+request.getCharacterEncoding());
                RunLogger.getLogger().log(Level.INFO,"RemoteHost："+request.getRemoteHost());
                RunLogger.getLogger().log(Level.INFO,"/*************请求内容信息*************/");
                //接收客户端的POST请求数据，输出为字符串格式
                /*StringBuilder sb = new StringBuilder();
                BufferedReader br = null;
                String line = null;
                try {
                    //接收request数据流，并指定编码格式接收
                    br = new BufferedReader(new InputStreamReader(request.getInputStream(), "UTF-8"));
                    while ((line = br.readLine()) != null) {
                        sb.append(line);
                    }
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                RunLogger.getLogger().log(Level.INFO,sb.toString());*/

                //解析客户端XML格式数据
                HIKEventBodyDecode.bodyDecoder(request.getInputStream());

                response.write("欢迎使用梵云大数据场景展示中心客流服务<br/>服务器接收数据成功.".getBytes());
            }
        });
        HttpBootstrap bootstrap = new HttpBootstrap();
        bootstrap.pipeline().next(routeHandle);
        bootstrap.setPort(8080).start();
    }
}
